<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12" >
            <a-form-model-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入姓名" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gender">
              <j-dict-select-tag type="list" v-model="model.gender" dictCode="sex" placeholder="请选择性别" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="出生日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="birthDate">
              <j-date placeholder="请选择出生日期" v-model="model.birthDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input v-model="model.phone" placeholder="请输入联系电话" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="身份证" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="idCard">
              <a-input v-model="model.idCard" placeholder="请输入身份证" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="籍贯" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="nativePlace">
              <a-input v-model="model.nativePlace" placeholder="请输入籍贯" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="民族" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="nation">
              <a-input v-model="model.nation" placeholder="请输入民族" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="最高学历" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="highestEduBackground">
              <a-input v-model="model.highestEduBackground" placeholder="请输入最高学历" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="毕业学校" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="graduationSchool">
              <a-input v-model="model.graduationSchool" placeholder="请输入毕业学校" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="申请职位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="positionApplied">
              <j-dict-select-tag type="list" v-model="model.positionApplied" dictCode="position_applied" placeholder="请选择申请职位" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="期望月薪" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="expectedSalary">
              <a-input v-model="model.expectedSalary" placeholder="请输入期望月薪" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="预期入职公司" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="expectedEntryCompany">
              <j-dict-select-tag type="list" v-model="model.expectedEntryCompany" dictCode="expected_company" placeholder="请选择预期入职公司" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="预计到岗时间（填写周数）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="estimatedTimeOfArrival">
              <a-input v-model="model.estimatedTimeOfArrival" placeholder="请输入预计到岗时间（填写周数）" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="工作简历" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="resume">
              <j-upload text = "点击上传（请务必上传已解密简历）" bizPath="简历归档/51job/51jobOld" v-model="model.resume"  ></j-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="获取渠道" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="accessChannels">
              <j-dict-select-tag type="list" v-model="model.accessChannels" dictCode="access_channels" placeholder="请选择获取渠道" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="猎头名称（如果选择猎头）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="headhunterName" v-show="model.accessChannels==1"> 
              <a-input v-model="model.headhunterName" placeholder="请输入猎头名称（如果选择猎头）" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="内部推送（如果选择内部推送）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="internalPush" v-show="model.accessChannels==2">
              <a-input v-model="model.internalPush" placeholder="请输入内部推送（如果选择内部推送）" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="资源共享" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="resourceSharing">
              <a-input v-model="model.resourceSharing" placeholder="请输入资源共享" ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="工作经历" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="workExperienceTable.loading"
          :columns="workExperienceTable.columns"
          :dataSource="workExperienceTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise,VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
  import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'BasicPersonnelFilesForm',
    mixins: [JEditableTableModelMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        model:{
            nation:"汉族",
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
           name: [
              { required: true, message: '请输入姓名!'},
           ],
           gender: [
              { required: true, message: '请输入性别!'},
           ],
           birthDate: [
              { required: true, message: '请输入出生日期!'},
           ],
           phone: [
              { required: true, message: '请输入联系电话!'},
           ],
           positionApplied: [
              { required: true, message: '请输入申请职位!'},
           ],
           expectedEntryCompany: [
              { required: true, message: '请输入入职公司!'},
           ],
           accessChannels: [
              { required: true, message: '请输入获取渠道!'},
           ],
        },
        refKeys: ['workExperience', ],
        tableKeys:['workExperience', ],
        activeKey: 'workExperience',
        // 工作经历
        workExperienceTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '开始时间',
              key: 'startTime',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '结束时间',
              key: 'endTime',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '公司名称',
              key: 'corporateName',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '内容简介',
              key: 'contentValidity',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '证明人',
              key: 'witness',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '基本人事档案id',
              key: 'basicPersonnelFilesId',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          add: "/ch/basicPersonnelFiles/add",
          edit: "/ch/basicPersonnelFiles/edit",
          queryById: "/ch/basicPersonnelFiles/queryById",
          workExperience: {
            list: '/ch/basicPersonnelFiles/queryWorkExperienceByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      addBefore(){
        this.workExperienceTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.workExperience.list, params, this.workExperienceTable)
        }
      },
      //校验所有一对一子表表单
      validateSubForm(allValues){
          return new Promise((resolve,reject)=>{
            Promise.all([
            ]).then(() => {
              resolve(allValues)
            }).catch(e => {
              if (e.error === VALIDATE_NO_PASSED) {
                // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
              } else {
                console.error(e)
              }
            })
          })
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          workExperienceList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

    }
  }
</script>

<style scoped>
</style>